package com.daiqee.controller;

import java.util.ArrayList;

import java.util.List;
import java.util.TreeMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.daiqee.controller.support.BaseController;
import com.daiqee.enums.RequestType;
import com.daiqee.pojo.Chart;
import com.daiqee.service.impl.ChartService;
import com.daiqee.vo.HighchartsVO;

/**
 * 报表
 * 
 * <br/>
 * @since JDK 1.8
 * @version  V2.4
 * @author RichardTang 
 * @date: 2018年6月9日14:40:05
 * @package com.daiqee.controller
 * @copyright:Copyright (c) 2018, 1245811923@qq.com All Rights Reserved.
 */
@Controller
@RequestMapping("chart")
public class ChartController extends BaseController{

	@Autowired
	protected ChartService chartService;
	
	/**
	 * 货物分类统计
	 */
	@RequestMapping(value = "/product-class",method = RequestMethod.GET)
	public Object queryProductClass() {
		List<Chart> result = chartService.queryProductClass();
		return restfulResponse.response(result,RequestType.GET);
	}
	
	/**
	 * 供应商统计
	 */
	@RequestMapping(value = "/supplier-class",method = RequestMethod.GET)
	public Object querySupplierClass() {
		List<Chart> result = chartService.querySupplierClass();
		return restfulResponse.response(result,RequestType.GET);
	}

	/**
	 * 查询生产订单情况和销售订单情况
	 */
	@RequestMapping(value = "/product-sale",method = RequestMethod.GET)
	public Object queryProductAndSale() {
		//用来响应的数据集合
		List<HighchartsVO<Integer[]>> responseList = new ArrayList<HighchartsVO<Integer[]>>();
		
		//销售订单情况统计
		List<Chart> saleResult = chartService.querySaleOrderCount();
		//生产订单情况统计
		List<Chart> productResult = chartService.queryProductionOrderCount();
		
		//格式化查询到的结果
		Integer[] formatAfterSaleResult = formatSQLResult(saleResult);
		Integer[] formatAfterProductResult = formatSQLResult(productResult);
		
		//包装成highcharts识别的JSON格式
		responseList.add(new HighchartsVO<Integer[]>("销售订单数",formatAfterSaleResult));
		responseList.add(new HighchartsVO<Integer[]>("生产订单数",formatAfterProductResult));
		
		return restfulResponse.response(responseList, RequestType.GET);
	}
	
	/**
	 * <p>查询员工数据</p>
	 * <p>每个月入职的员工数</p>
	 * <p>每个月的总员工数</p>
	 * <p>每个月的离职人数</p>
	 */
	@RequestMapping(value = "/employee",method = RequestMethod.GET)
	public Object queryEmployeeData() {
		//用来响应的数据集合
		List<HighchartsVO<Integer[]>> responseList = new ArrayList<HighchartsVO<Integer[]>>();
		//入职员工数
		List<Chart> newResult = chartService.queryEveryMonthOfNewEmployee();
		//离职员工人数
		List<Chart> quitResult = chartService.queryEveryMontOfQuitEmployee();
		//格式化后12个月份的新员工入职人数
		Integer[] formatAfterNewResult = formatSQLResult(newResult);
		//格式化12个月中离职的员工数据
		Integer[] formatAfterQuitResult = formatSQLResult(quitResult);
		//根据员工的入职人数来计算出每个月的总人数
		Integer[] sumEveryMonthEmployeeNumber = new Integer[12];
		//记数用
		Integer cacheNumber = 0;
		for(int i = 0;i < 12;i++) {
			cacheNumber = cacheNumber + formatAfterNewResult[i];
			sumEveryMonthEmployeeNumber[i] = cacheNumber;
		}
		
		//包装成highcharts识别的JSON格式
		responseList.add(new HighchartsVO<Integer[]>("入职人数",formatAfterNewResult));
		responseList.add(new HighchartsVO<Integer[]>("总人员数",sumEveryMonthEmployeeNumber));
		responseList.add(new HighchartsVO<Integer[]>("离职人数", formatAfterQuitResult));
		
		return restfulResponse.response(responseList, RequestType.GET);
	}
	
	/**
	 * <p>将查询到的结果转换成-Integer[]的形式,这里的Integer是用来表示highcharts图表的月份对应的数据</p>
	 * @param result
	 * 		从后台查询到的结果
	 */
	public Integer[] formatSQLResult(List<Chart> result) {
		
		//格式化数据,先封装成Map才好对数据进行区分 key:月份 value:生产订单数,这个是用来临时存储的,相当于是一个字典
		//查询该字典中书否有这个值
		TreeMap<String,String> tempOrderResult = new TreeMap<String,String>();
		
		//遍历查询到的数据
		for(Chart crmo: result) {
			tempOrderResult.put(crmo.getName(), crmo.getNumber());
		}
		//这个是需要输出成json的数组,里面封装了,每个月的订单数,这里是12个月所以大小写12
		Integer[] orderNumber = new Integer[12];
		
		//循环12次,通过i值去tempOrderResult字典中查询,是否存在该值.如果存在着取出 getNumber(); 不存在就直接赋值0
		for(int i = 1;i < 13; i++) {
			
			//i就相当于月份也就是tempOrderMap中的key,通过i拿到number
			String monthOfNumber = tempOrderResult.get(String.valueOf(i));
			//如果monthOfNumber=null,代表该月份没有订单数据,直接赋值为0
			if(monthOfNumber==null) {
				//存储到orderNumber数组中
				orderNumber[i-1] = 0;
			}else {
				//有值,直接存储
				orderNumber[i-1] = Integer.parseInt(monthOfNumber);
			}
		}
		return orderNumber;
	}
	
}
